﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Office;

namespace ReportDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public static string FilePath = "C:\\Users\\reisen\\Desktop\\test.xlsx";
        public event PropertyChangedEventHandler PropertyChanged;
        ObservableCollection<Data> _Datas = new ObservableCollection<Data>();
        ExportToExcel ex = new ExportToExcel();
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            _Datas.Add(new Data() { Field1 = "xxx", Field2 = "yyy" });
            _Datas.Add(new Data() { Field1 = "xxx", Field2 = "yyy" });
            _Datas.Add(new Data() { Field1 = "xxx", Field2 = "yyy" });
            _Datas.Add(new Data() { Field1 = "xxx", Field2 = "yyy" });
            List<Data> list = new List<Data>(_Datas.ToList());
            DataTable newTB = Datavalidation.CopyToDataTable<Data>(list);
            
        }

        public void ClickExport(object sender, RoutedEventArgs e)
        {
            ex.Export(DataGrid01, "Sheet1");
        }
        public void ClickAddRow(object sender, RoutedEventArgs e)
        {
            _Datas.Add(new Data());
        }
        public ObservableCollection<Data> Datas
        {
            get => _Datas;
            set
            {
                _Datas = value;
                if (_Datas != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(nameof(Datas)));
                }
            }
        }

    }
    public static class Datavalidation
    {
        public static DataTable CopyToDataTable<T>(this IEnumerable<T> array)
        {
            var ret = new DataTable();
            foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))
                ret.Columns.Add(dp.Name);
            foreach (T item in array)
            {
                var Row = ret.NewRow();
                foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))
                    Row[dp.Name] = dp.GetValue(item);
                ret.Rows.Add(Row);
            }
            return ret;
        }
    }

    /// <summary>
    /// 数据记录
    /// </summary>
    public class Data:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string _Field1 = "";
        private string _Field2 = "";
        private string _Field3 = "";
        // 想增加新的字段就往这里新加成员以及CLR属性暴露
        public string Field1
        {
            get => _Field1;
            set
            {
                _Field1 = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(nameof(Field1)));
                }
            }
        }
        public string Field2
        {
            get => _Field2;
            set
            {
                _Field2 = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(nameof(Field2)));
                }
            }
        }
        public string Field3
        {
            get => _Field3;
            set
            {
                _Field3 = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(nameof(Field3)));
                }
            }
        }
    }
}
